/**
 * Copyright：2020，TLF工作室。
 */
package com.cqeec.netdisc.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.cqeec.netdisc.model.User;
import com.cqeec.netdisc.service.FileService;

/**
 * 上传文件控制器。
 * 
 * @author 唐礼飞
 * 
 * @date 2020-11-03 14:36:47
 *
 */
@RestController
public class UploadFileController {
	@Autowired
	private FileService fileService;

	@PostMapping("/upload/file")
	public Map<String, Object> upload(MultipartFile[] files, HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		Object obj = session.getAttribute("user");
		if (obj == null) {
			// 获取当前用户
			map.put("code", 1);
			map.put("message", "用户信息丢失！");
		} else {
			User user = (User) obj;
			// 根据当前用户将文件传入对应的容器
			for (MultipartFile mf : files) {
				try {
					fileService.uploadFile(user.getLoginName(),mf.getOriginalFilename(),mf.getInputStream());
				} catch (IOException e) {
					e.printStackTrace();
					map.put("code", 2);
					map.put("message", "读取上传文件的内容失败！");
				}
			}
			map.put("code", 0);
			map.put("message", "上传文件成功！");
		}
		return map;
	}
}
